import { popTarget, pushTarget } from "./dep";
import { queueWatcher } from "./scheduler";

let id = 0;

class Watcher {
    constructor(vm, exprOrFn, cb, options) {
        this.vm = vm;
        this.exprOrFn = exprOrFn;
        this.cb = cb;
        this.options = options;
        this.id = id++;
        this.deps = []; // 存放 dep 的容器
        this.depsId = new Set(); // 去重 dep
        if(typeof exprOrFn === 'function') {
            this.getter = exprOrFn;
        }
        this.get();
    }
    get() {
        pushTarget(this);
        this.getter();
        popTarget();
    }
    addDep(dep) {
        let id = dep.id;
        if(!this.depsId.has(id)) {
            this.depsId.add(id);
            this.deps.push(dep);
            dep.addSub(this);
        }
    }
    update () {
        // this.get();
        queueWatcher(this);
    }
    run() {
        this.get();
    }
}
export default Watcher;